package com.nuyoah.config;


import com.nuyoah.security.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    LoginFailureHandler loginFailureHandler; // 失败处理器
    @Autowired
    LoginSuccessHandler loginSuccessHandler; // 成功处理器
    @Autowired
    CaptchaFilter captchaFilter; // 验证码过滤器
    @Autowired
    JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; // JWT认证入口点
    @Autowired
    JwtAccessDeniedHandler jwtAccessDeniedHandler; // JWT访问被拒绝处理器
    @Autowired
    UserDetailServiceImpl userDetailService; // 用户详情服务
    @Autowired
    JwtLogoutSuccessHandler jwtLogoutSuccessHandler; // JWT退出成功处理器

    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        return new JwtAuthenticationFilter(authenticationManager());
    }


    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    public static final String[] URL_WHITELIST = {
            "/webjars/**",
            "/login",
            "/logout",
            "/captcha",
            "/favicon.ico",
            "/sys/activity/uploadImage",
            "/api/file/**",
            "/sys/activity/list/user",
            "/sys/userInfo",
            "/sys/activity/export/{id}",
            "/sys/user/register"
    };

    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and().csrf().disable()
                /*.authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated();*/

                // 登录配置
                .formLogin()
                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailureHandler)

                .and()
                .logout()
                .logoutSuccessHandler(jwtLogoutSuccessHandler)

                // 禁用session
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                // 配置拦截规则
                .and()
                .authorizeRequests()
                .antMatchers(URL_WHITELIST).permitAll()
                .anyRequest().authenticated()

                // 异常处理器
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .accessDeniedHandler(jwtAccessDeniedHandler)

                // 配置自定义的过滤器
                .and()
                .addFilter(jwtAuthenticationFilter())
                .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class);
    }

}
